package utils
{
	import com.greensock.TweenMax;
	import com.greensock.TweenNano;
	import com.greensock.easing.Back;
	import com.greensock.easing.Cubic;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import interfaces.ILayoutAnimationFrame;
	
	import mvc.views.frames.FramePanel;

	public class UtilsAnimation
	{
		/**
		 * 面板动画1
		 * @param _startFrame
		 * @param _endFrame
		 */
		public static function showLayoutAnimation1(_startFrame:FramePanel,_endFrame:FramePanel,_onComplete:Function):void
		{
			TweenMax.to(_startFrame,0.3,{colorTransform:{tint:0xffffff, tintAmount:2, exposure:2, brightness:0},ease:Back.easeIn,onComplete:function():void{
				TweenMax.to(_startFrame, 0.2, {alpha:0, onComplete:function():void{
					_endFrame.visible=true;
					//渐变之前的面板
					TweenNano.to(_startFrame,0.2,{alpha:0});
					//设置新面板的状态
					TweenMax.to(_endFrame,0,{alpha:0,colorTransform:{tint:0xffffff, tintAmount:2, exposure:2, brightness:0}});
					//用scale来恢复原样比直接设置长宽稳定,不然有可能会严重变形
					TweenMax.to(_endFrame,0.3,{onComplete:function():void{
						_onComplete.call();
						//渐变新面板到正常状态
						TweenMax.to(_endFrame,0.3,{alpha:1,colorTransform:{tint:0xffffff, tintAmount:0, exposure:0, brightness:0}});
					}});
				}});				
			}});
		}
		
		/**
		 * 显示面板样式1
		 * @param _target
		 */
		public static function showFrameStyle1(_target:DisplayObject,_fromAlpha:int=0):void
		{
			_target.alpha = _fromAlpha;
			
			TweenMax.to(_target,0.5,{alpha:1,colorTransform:{tint:0xffffff, tintAmount:2, exposure:2, brightness:0}, ease:Back.easeIn,onComplete:function():void{
				TweenMax.to(_target,0.5,{colorTransform:{tint:0xffffff, tintAmount:0, exposure:0, brightness:0},ease:Back.easeOut});
			}});
		}
		
		/**
		 * 震动效果
		 * @param dis
		 * @param times
		 * @param offset
		 * @param speed
		 */
		private static var isShake:Boolean = false;
		public static function shake(dis:DisplayObject,times:uint = 2,offset:uint = 4,speed:uint = 32):void 
		{ 
			if(isShake) {  
				return;  
			}  
			isShake = true;  
			var point:Point = new Point(dis.x,dis.y);  
			var offsetXYArray:Array = [0,0];  
			var num:int = 0;  
			var u:int = setInterval
				(
					function():void
					{  
						offsetXYArray[num%2] = (num++)%4<2 ?0:offset;  
						if(num>(times*4 + 1)){  
							clearInterval(u);  
							num = 0;  
							isShake = false;
						}  
						dis["x"] = offsetXYArray[0] + point.x;  
						dis["y"] = offsetXYArray[1] + point.y;  
					},speed
				);  
		}
	}
}